package com.fsck.k9.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.view.ViewGroup;

import com.yunshipei.common.Globals;
import com.yunshipei.common.manager.EnterConfig;
import com.yunshipei.core.ui.view.WaterMarkView;
import com.yunshipei.core.utils.SystemUtils;
import com.yunshipei.model.WaterMarkSettings;

public class K9BasePreferencesActivity extends PreferenceActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            this.getWindow().setStatusBarColor(EnterConfig.getInstance().getStatusBarGlobalColor());
        }
        registerReceiver(activityManagerReceiver, new IntentFilter(Globals.ACTION_FINISH_BASE_ACTIVITY));
        if (SystemUtils.isPad(this)) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        initWatermark();
    }

    private boolean initWtm = false;

    protected void initWatermark() {
        WaterMarkSettings waterMarkSettings = EnterConfig.getInstance().getWaterMarkSettings();
        if (waterMarkSettings != null && waterMarkSettings.getType() == 0 && !initWtm) {
            initWtm = true;
            ViewGroup rootView = (ViewGroup) findViewById(android.R.id.content);
            if (rootView != null) {
                WaterMarkView waterMarkView = new WaterMarkView(this);
                waterMarkView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                waterMarkView.updateWaterMarkConfig(waterMarkSettings.getWaterMarkConfig());
                rootView.addView(waterMarkView);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(activityManagerReceiver);
    }

    private BroadcastReceiver activityManagerReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            ((Activity) context).finish();
        }
    };
}
